const prefix = 'FUNNYBOX-',
  musicRankings = `${prefix}MUSICRANKINGS`,
  searchMusicHis = `${prefix}SEARCHMUSICHIS`;

const auth = {
  // 设置音乐专辑信息，避免刷新页面丢失store
  setMusicRankings(obj: object) {
    const data = JSON.stringify(obj);
    localStorage.setItem(musicRankings, data);
  },
  // 获取音乐专辑信息
  getMusicRankings() {
    const data: string | null = localStorage.getItem(musicRankings);
    return JSON.parse(typeof data === 'string' ? data : '{}');
  },

  // 设置音乐搜索历史
  setMusicSearch(name: string) {
    let storageHis = auth.getMusicSearchHis();
    storageHis.push(name);

    localStorage.setItem(searchMusicHis, JSON.stringify(storageHis));
  },
  // 获取音乐搜索历史
  getMusicSearchHis() {
    const data: string | null = localStorage.getItem(searchMusicHis);
    if (data) {
      return JSON.parse(data);
    } else {
      return [];
    }
  },
};

export default auth;
